package com.franklin.ideaplugin.easytesting.action;

import com.franklin.ideaplugin.easytesting.icon.PluginIcons;
import com.franklin.ideaplugin.easytesting.ui.RunMethodForm;
import com.franklin.ideaplugin.easytesting.ui.Sizes;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.popup.*;
import com.intellij.psi.PsiMethod;
import org.jetbrains.annotations.NotNull;

/**
 * @author Ye Junhui
 * @since 2023/5/12
 */
public class RunMethodAction extends AnAction {

    private PsiMethod psiMethod;

    public RunMethodAction(PsiMethod psiMethod) {
        this.psiMethod = psiMethod;
    }

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        //生成表单
        RunMethodForm runMethodForm = new RunMethodForm(psiMethod);
        JBPopupFactory jbPopupFactory = JBPopupFactory.getInstance();
        ComponentPopupBuilder componentPopupBuilder = jbPopupFactory.createComponentPopupBuilder(runMethodForm.getMainPanel(), runMethodForm.getMainPanel());
        JBPopup runMethodPopup = componentPopupBuilder
                .setTitle("Run Method")
                .setCancelKeyEnabled(true)
                .setCancelButton(new IconButton("close", AllIcons.Actions.CloseHovered))
                .setTitleIcon(new ActiveIcon(PluginIcons.PLUGIN_ICON))
                .setMovable(true)
                .setShowShadow(true)
                .setResizable(true)
                .setRequestFocus(true)
                .setCancelOnClickOutside(false)
                .createPopup();

        runMethodPopup.setSize(Sizes.RunMethodFormSize.MAIN_SIZE);
        runMethodPopup.showCenteredInCurrentWindow(psiMethod.getProject());
        runMethodForm.setCancelWorker(runMethodPopup::cancel);
    }
}
